//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "tools\fn_getCrewType.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_side","_entry","_type","_crew","_typcargo","_override","_value_name","_sim"];
DEFAULT_PARAM(_side,0,GVAR(side_enemy));
DEFAULT_PARAM(_entry,1,[]);
DEFAULT_PARAM(_type,2,"");
_result = [];

if (IS_SIDE(_side) && IS_CONFIG(_entry) && AND_ALSO(IS_STRING(_type),_type isKindOf "AllVehicles")) then {
	_crew = "";
	_typcargo = [];
	_crew = getText (_entry >> "crew");
	
	_value_name = format["VEHICLE_CREW_%1",toUpper(str(_side))];
	_sim = getText(configFile >> "CfgVehicles" >> _type >> "simulation");
	if (_sim in ["airplane", "helicopter"]) then {
		_value_name = format["VEHICLE_PILOT_%1",toUpper(str(_side))];
	};
	_override = [_value_name,_crew] call FFUNC(getFrameworkValue);
	if (IS_ARRAY(_override)) then {
		if (count _override > 0) then {
			_override = _override call FFUNC(arrayRandomVal);
		} else {
			_override = "";
		};
	};
	TRACE_1("Crew",ARR_2(_crew,_override));
	if (_override != "") then { _crew = _override };
	if (_crew == "") then {
		_value_name = format["VEHICLE_CREW_DEFAULT_%1",toUpper(str(_side))];
		_crew = [_value_name,_crew] call FFUNC(getFrameworkValue);
	};
	TRACE_1("Crew",_crew);
	_typcargo = (getArray (_entry >> "typicalCargo")) - [_crew,"Soldier"];
	TRACE_1("Cargo",_typcargo);
	if (count _typcargo > 0) then {
		_value_name = format["VEHICLE_CREW_CARGO_%1",toUpper(str(_side))];
		_override = [_value_name,[]] call FFUNC(getFrameworkValue);
		TRACE_1("Cargo override",_override);
		if ((count _override > 0) && (count _typcargo > 0)) then {
			for "_i" from 0 to ((count _typcargo) - 1) step 1 do {
				_typcargo set [ _i, (_override call FFUNC(arrayRandomVal))];
			};
		};
	};

	_result = [_crew, _typcargo]
};
TRACE_1("End",_result);
TMFPROFILERSTOP;
_result;